﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RDPCommon.Client
{
    public class ClientTool
    {
        /// <summary>
        /// 每启动一个客户端示例的唯一标志
        /// </summary>
        public static RDPCommon.Client.ClinetInfo UserClinetInfo = null;

        static RDPCommon.IUserLogin userLogin;
        static RDPCommon.EventWrapper wrapper;
        static RDPCommon.IRemoteCall remoteCall;

        public static object[] CallServerMethod(string fCase,string fAssembly,string fClass,string fMethod,object[] _parms)
        {
            object[] rtn = new object[] { 0, "", null };
            object[] _robjs = new object[] { fCase, fAssembly, fClass, fMethod };
            object[] resp = new object[] { };
            try
            {
                if (remoteCall == null)
                    remoteCall = (RDPCommon.IRemoteCall)RDPCommon.Client.RemotingHelper.GetObject(typeof(RDPCommon.IRemoteCall));
                resp = remoteCall.CallRemote(_robjs, _parms);
                if (resp != null)
                    return resp[0] as object[];
            }
            catch (Exception ex)
            {
                rtn[0] = 1;
                rtn[1] = ex;
            }
            return rtn;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="ci"></param>
        /// <returns></returns>
        public static bool UserLogin(RDPCommon.Client.ClinetInfo ci)
        {
            try
            {
                userLogin = (RDPCommon.IUserLogin)RDPCommon.Client.RemotingHelper.GetObject(typeof(RDPCommon.IUserLogin));
                object[] rtn = userLogin.Login(ci);
                if (rtn != null && Convert.ToInt32(rtn[0]) != 0)
                {
                    Exception ex = rtn[1] as Exception;
                    throw ex;                    
                }
                else
                {
                    wrapper = new RDPCommon.EventWrapper();
                    //注册在线测试
                    wrapper.LocalOnlienTestEvent += new RDPCommon.OnlienTestEventHandler(OnlienTest);
                    userLogin.AddEventReappear(UserClinetInfo.SessionID, wrapper.OnlienTest);  

                    ////注册用户登入通知
                    //wrapper.LocalLoginNotifyEvent += new RDPCommon.LoginNotifyEventHandler(OnLoginNotify);
                    //userLogin.AddLoginNotifyEvent(UserClinetInfo.SessionID, wrapper.OnLoginNotify);
                    ////注册用户注销通知
                    //wrapper.LocalLogoutNotifyEvent += new RDPCommon.LogoutNotifyEventHandler(OnLogoutNotify);
                    //userLogin.AddLogoutNotifyEvent(UserClinetInfo.SessionID, wrapper.OnLogoutNotify);
                }
                return true;
            }
            catch (Exception ex)
            {
                userLogin = null;
                throw ex;       
            }            
        }

        /// <summary>
        /// 用户注销
        /// </summary>
        /// <param name="ci"></param>
        /// <returns></returns>
        public static bool UserLogout(RDPCommon.Client.ClinetInfo ci)
        {
            if (userLogin == null) //此时用户还没登录
                return true;
            try
            {                
                object[] rtn = userLogin.Logout(ci);
                if (rtn != null && Convert.ToInt32(rtn[0]) != 0)
                {
                    Exception ex = rtn[1] as Exception;
                    throw ex;
                }
                else
                {
                    userLogin.RemoveEventReappear(UserClinetInfo.SessionID, wrapper.OnlienTest);
                    //userLogin.RemoveLoginNotifyEvent(UserClinetInfo.SessionID, wrapper.OnLoginNotify);
                    //userLogin.RemoveLogoutNotifyEvent(UserClinetInfo.SessionID, wrapper.OnLogoutNotify);
                }                
                return true;
            }
            catch (Exception ex)
            {                
                throw ex;       
            }            
        }

        /// <summary>
        /// 供服务商测试客户端是否在线使用
        /// </summary>
        /// <param name="ci"></param>
        static void OnlienTest(RDPCommon.Client.ClinetInfo ci)
        {
            //此处忽略
        }

        //static void OnLoginNotify(RDPCommon.Client.ClinetInfo ci)
        //{
        //    //此处忽略
        //}

        //static void OnLogoutNotify(RDPCommon.Client.ClinetInfo ci)
        //{
        //    //此处忽略
        //}

        /// <summary>
        /// 返回服务端在线用户
        /// </summary>
        /// <param name="objs"></param>
        /// <returns></returns>
        public static object[] GetServerActiveUsers(object[] objs)
        {
            object[] rtn = new object[] { 0, "", null };
            object[] resp = null;
            try
            {
                resp = CallServerMethod(
                    "", "RDPSystem", "RDPSystem.ServerObject", "GetServerActiveUsers", new object[] { });
                return resp;
            }
            catch (Exception ex)
            {
                rtn[0] = 1;
                rtn[1] = ex;
            }
            return rtn;
        }

        /// <summary>
        /// 返回服务端帐套
        /// </summary>
        /// <param name="objs"></param>
        /// <returns></returns>
        public static object[] GetServerDB(object[] objs)
        {
            object[] rtn = new object[] { 0, null };
            object[] resp = null;
            try
            {
                resp = CallServerMethod(
                    "", 
                    "RDPSystem", 
                    "RDPSystem.ServerObject", 
                    "GetServerDB", new object[] { });
                return resp;
            }
            catch (Exception ex)
            {
                rtn[0] = 1;
                rtn[1] = ex;
            }
            return rtn;
        }
    } 
}
